/*
 *  Copyright 2015-2016, RenGu Technology (BeiJing) Co., Ltd.
 *  All right reserved.
 *
 *  THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED TO THIR PARTIES,
 *  COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 *  WITHOUT THE PRIOR WRITTEN PERMISSION OF RENGU TECHNOLOGY (BeiJing) CO., LTD.
 *
 *  QueryAcctInfo3o1
 *
 *  Edit History:
 *      lizhangtao Created on 2020年04月12日
 *
 */
package cc.rengu.igas.acct.common.util;

import cc.rengu.igas.acct.common.dao.AcctInfoMapper;
import cc.rengu.igas.acct.common.dao.impl.AcctInfoMapperImpl;
import cc.rengu.igas.acct.common.entity.AcctInfo;
import cc.rengu.igas.acct.common.enums.AcctRespCodeEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.utility.base.StringUtil;
import cc.rengu.utility.log.RgLogger;

/**
 * @author lizhangtao
 * @version 1.0
 * @date 2020/5/8
 */
public class QueryAcct3o1 {

    /**
     * 使用账号、客户号、用户类型&用户id三个选项中一个查询账户信息
     * 若上送了多个，则有账号时使用账号查询，其余的需要校验
     * 若上送了多个但是没有账号，则有客户号时使用客户号查询，其余的需要校验
     *
     * @param instId     机构号
     * @param acctNo     账号
     * @param customerId 客户号
     * @param userType   外部用户类型
     * @param userId     外部用户id
     * @param dbsUtil    数据操作句柄
     * @param rglog      日志句柄
     * @return 成功返回账户信息，失败或者无数据抛出异常
     * @throws BizException 失败或者无数据抛出异常
     */
    public static AcctInfo queryAcctInfo(String instId, String acctNo, String customerId,
                                         String userType, String userId, DbsUtil dbsUtil, RgLogger rglog) throws BizException {
        AcctInfoMapper acctInfoMapper = new AcctInfoMapperImpl();
        AcctInfo acctInfo;
        try {
            //以账号查询
            if (!StringUtil.isNullorEmpty(acctNo)) {
                acctInfo = acctInfoMapper.selectAcctInfoByAcctNo(instId, acctNo, dbsUtil);
                if (null == acctInfo) {
                    rglog.error("使用账号<{}>查询不到账户", acctNo);
                    throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                            AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
                }
            }
            //以客户号查询
            else if (!StringUtil.isNullorEmpty(customerId)) {
                acctInfo = acctInfoMapper.selectAcctInfoByCustomerId(instId, customerId, dbsUtil);
                if (null == acctInfo) {
                    rglog.error("使用客户号<{}>查询不到账户", customerId);
                    throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                            AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
                }
            }
            //以外部用户和用户标识查询
            else if (!StringUtil.isNullorEmpty(userId) && !StringUtil.isNullorEmpty(userType)) {
                acctInfo = acctInfoMapper.selectAcctInfoByUserId(instId, userType, userId, dbsUtil);
                if (null == acctInfo) {
                    rglog.error("使用外部用户类型<{}>用户标识<{}>查询不到账户", userType, userId);
                    throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                            AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
                }
            } else {
                rglog.error("账号|客户号|外部用户类型&用户标识至少上送其一！");
                throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                        AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        } catch (BizException bizException) {
            throw bizException;
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            throw new BizException(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode(),
                    OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
        }
        return acctInfo;
    }

    /**
     * 锁定账号
     *
     * @param instId     机构号，必须上送
     * @param acctNo     账号
     * @param customerId 客户号
     * @param userType   外部用户类型
     * @param userId     外部用户id
     * @param dbsUtil    数据库操作句柄
     * @param rglog      日志句柄
     * @return 成功返回账户信息、失败或者无数据抛出异常
     * @throws BizException 失败或者无数据抛出异常
     */
    public static AcctInfo lockAcctInfo(String instId, String acctNo, String customerId,
                                        String userType, String userId, DbsUtil dbsUtil, RgLogger rglog) throws BizException {
        AcctInfoMapper acctInfoMapper = new AcctInfoMapperImpl();
        AcctInfo acctInfo;
        try {
            //以账号锁定
            if (!StringUtil.isNullorEmpty(acctNo)) {
                acctInfo = acctInfoMapper.lockAcctInfoByAcctNo(instId, acctNo, dbsUtil);
                if (null == acctInfo) {
                    rglog.error("使用账号<{}>查询不到账户", acctNo);
                    throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                            AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
                }
            }
            //以客户号锁定
            else if (!StringUtil.isNullorEmpty(customerId)) {
                acctInfo = acctInfoMapper.lockAcctInfoByCustomerId(instId, customerId, dbsUtil);
                if (null == acctInfo) {
                    rglog.error("使用客户号<{}>查询不到账户", customerId);
                    throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                            AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
                }
            }
            //以外部用户和用户标识锁定
            else if (!StringUtil.isNullorEmpty(userId) && !StringUtil.isNullorEmpty(userType)) {
                acctInfo = acctInfoMapper.lockAcctInfoByUserId(instId, userType, userId, dbsUtil);
                if (null == acctInfo) {
                    rglog.error("使用外部用户类型<{}>用户标识<{}>查询不到账户", userType, userId);
                    throw new BizException(AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespCode(),
                            AcctRespCodeEnum.ACCT_NOT_EXIST_ERROR.getRespDesc());
                }
            } else {
                rglog.error("账号|客户号|外部用户类型&用户标识至少上送其一！");
                throw new BizException(AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(),
                        AcctRespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        } catch (BizException bizException) {
            throw bizException;
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            throw new BizException(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode(),
                    OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
        }
        return acctInfo;
    }
}
